//
//  CZRetweetView.swift
//  sinaSwifWeiBo
//
//  Created by 张雪雪 on 16/3/5.
//  Copyright © 2016年 张雪雪. All rights reserved.
//

import UIKit
import SnapKit

class CZRetweetView: UIView {
    
    //定义一个属性来记录全局的约束
    
    var bottomConstaint: Constraint?

    var retweetViewModel: CZStatusViewModel?{
    
        didSet{
        
        contentLabel.text = retweetViewModel?.retweetStatusString
        //清空原有约束防止清空约束
            self.bottomConstaint?.uninstall()
            
            if let pic_url = retweetViewModel?.statusModel?.retweeted_status?.pic_urls where pic_url.count > 0{
            
            pictureView.hidden = false
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                //需要把model 转换成viewmodel
                //定义一个临时数组进行数据拼接
                var result = [CZPictureViewModel]()
                
                for model in pic_url{
                
                    let viewModel = CZPictureViewModel()
                    
                    viewModel.model = model
                    
                    result.append(viewModel)
                
                }
                
                pictureView.pictureList = result
                
                
            }else{
            
            pictureView.hidden = true
                
                //跟新约束
                
            self.snp_updateConstraints(closure: { (make) -> Void in
                self.bottomConstaint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
            })
            
            
            }
            
        }
    
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
//放置崩溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//MARK:添加控件和约束
    private func setupUI(){
    
    backgroundColor = UIColor.cyanColor()
    
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
       //     make.bottom.equalTo(self.snp_bottom)
        }
        //默认都有转发配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom)
            
            //给他一个固定的宽高
            //make.size.equalTo(CGSizeMake(100, 100))
        }
        
        //转发视图显示在哪里
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
        
    
    }
    
    //懒加载
    private lazy var contentLabel: UILabel = {
    
    let label = UILabel()
        
        label.text = "转发微博"
        label.textColor = UIColor.redColor()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        return label
    }()
    
    //
    lazy var pictureView:CZPictureView = CZPictureView()
    
    
}
